// 
//  Copyright (C) 2009 Jacek Trubłajewicz
// 
//  This program is free software; you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation; either version 2 of the License, or
//  (at your option) any later version.
// 
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
//  GNU General Public License for more details.
//  
//  You should have received a copy of the GNU General Public License
//  along with this program; if not, write to the Free Software
//  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
// 

using System;
using System.Drawing;
using HAKGERSoft;
using Mono.Unix;
using NLog;

namespace wxGadu
{
		
	public class BehaviourSettingsPanel : wx.Panel
	{
		#region Id
		public enum Id
		{
			fileLoggingCombo = 1632,
			consoleLoggingCombo,
			mainPosXSpin,
			mainPosYSpin,
			chatPosXSpin,
			chatPosYSpin,
			tooltipsDelaySpin,
			enterSendCheck,
			enableTrayCheck,
			showTooltipsCheck,
			confirmExitCheck,
			closeToTrayCheck,
			showNewChatCheck,
			saveHistoryCheck,
			notifyTtlSpin,
			chatMessageCountSpin
		}
		#endregion
		
		#region controls
		private wx.StaticText fileLoggingST;
		private wx.StaticText consoleLoggingST;
		private wx.StaticText tooltipsDelayST;
		private wx.StaticText mainPosXST;
		private wx.StaticText mainPosYST;
		private wx.StaticText chatPosXST;
		private wx.StaticText chatPosYST;
		private wx.StaticText notifyTtlST;
		private wx.StaticText chatMessageCountST;
		private wx.ComboBox fileLoggingCombo;
		private wx.ComboBox consoleLoggingCombo;
		private wx.SpinCtrl mainPosXSpin;
		private wx.SpinCtrl mainPosYSpin;
		private wx.SpinCtrl chatPosXSpin;
		private wx.SpinCtrl chatPosYSpin;
		private wx.SpinCtrl tooltipsDelaySpin;
		private wx.SpinCtrl notifyTtlSpin;
		private wx.SpinCtrl chatMessageCountSpin;
		private wx.CheckBox enterSendCheck;
		private wx.CheckBox enableTrayCheck;
		private wx.CheckBox confirmExitCheck;
		private wx.CheckBox showTooltipsCheck;
		private wx.CheckBox closeToTrayCheck;
		private wx.CheckBox showNewChatCheck;
		private wx.CheckBox saveHistoryCheck;
		private wx.StaticBox loggingBox;
		private wx.StaticBox positionBox;
		private wx.StaticBox othersBox;
		private wx.StaticBox mainBox;
		private wx.StaticBox chatBox;
		#endregion
		
		#region sizers
		private wx.BoxSizer topSizer;
		private wx.BoxSizer boxSizer;
		private wx.StaticBoxSizer loggingSizer;
		private wx.StaticBoxSizer positionSizer;
		private wx.StaticBoxSizer othersSizer;
		private wx.StaticBoxSizer mainSizer;
		private wx.StaticBoxSizer chatSizer;
		private wx.FlexGridSizer loggingGrid;
		private wx.FlexGridSizer mainGrid;
		private wx.FlexGridSizer chatGrid;
		private wx.FlexGridSizer othersGrid;
		#endregion
		
		
		#region data members
#if LOG
		private Logger logger = LogManager.GetCurrentClassLogger();	
#endif
		#endregion
		
		public BehaviourSettingsPanel(wx.Window parent, int id, Point pos, Size size, uint style):
			base(parent, id, pos, size, style)
		{
			DoControls();
			DoControlsProperties();
			DoLayout();
			
			EVT_UPDATE_UI((int) Id.tooltipsDelaySpin, new wx.EventListener(OnTooltipsSpinUI));
		}
		
		private void DoControls()
		{
			fileLoggingST = new wx.StaticText(this, Catalog.GetString("File logging level:"));
			consoleLoggingST = new wx.StaticText(this, Catalog.GetString("Console logging level:"));
			tooltipsDelayST = new wx.StaticText(this, Catalog.GetString("Tooltips delay [ms]:"));
			mainPosXST = new wx.StaticText(this, Catalog.GetString("X:"));
			mainPosYST = new wx.StaticText(this, Catalog.GetString("Y:"));
			chatPosXST = new wx.StaticText(this, Catalog.GetString("X:"));
			chatPosYST = new wx.StaticText(this, Catalog.GetString("Y:"));
			notifyTtlST = new wx.StaticText(this, Catalog.GetString("Show notify window for [ms]:"));
			chatMessageCountST = new wx.StaticText(this, Catalog.GetString("Number of messages in chat window:"));
			fileLoggingCombo = new wx.ComboBox(this, (int) Id.fileLoggingCombo, string.Empty,
			                                   wx.Window.wxDefaultPosition, wx.Window.wxDefaultSize,
			                                   new string[0], wx.ComboBox.wxCB_READONLY);
			consoleLoggingCombo = new wx.ComboBox(this, (int) Id.consoleLoggingCombo, string.Empty,
			                                   wx.Window.wxDefaultPosition, wx.Window.wxDefaultSize,
			                                   new string[0], wx.ComboBox.wxCB_READONLY);
			wx.Display disp = wx.Display.GetDisplay(0);
			mainPosXSpin = new wx.SpinCtrl(this, (int) Id.mainPosXSpin, string.Empty,
			                               wx.Window.wxDefaultPosition, wx.Window.wxDefaultSize,
			                               0, 0, disp.Geometry.Width, 0);
			mainPosYSpin  = new wx.SpinCtrl(this, (int) Id.mainPosYSpin, string.Empty,
			                               wx.Window.wxDefaultPosition, wx.Window.wxDefaultSize,
			                               0, 0, disp.Geometry.Height, 0);
			chatPosXSpin = new wx.SpinCtrl(this, (int) Id.chatPosXSpin, string.Empty,
			                               wx.Window.wxDefaultPosition, wx.Window.wxDefaultSize,
			                               0, 0, disp.Geometry.Width, 0);
			chatPosYSpin  = new wx.SpinCtrl(this, (int) Id.chatPosYSpin, string.Empty,
			                               wx.Window.wxDefaultPosition, wx.Window.wxDefaultSize,
			                               0, 0, disp.Geometry.Height, 0);
			tooltipsDelaySpin = new wx.SpinCtrl(this, (int) Id.tooltipsDelaySpin, string.Empty,
			                               wx.Window.wxDefaultPosition, wx.Window.wxDefaultSize,
			                               0, 0, 10000, 1000);
			notifyTtlSpin = new wx.SpinCtrl(this, (int) Id.notifyTtlSpin, string.Empty,
			                                wx.Window.wxDefaultPosition, wx.Window.wxDefaultSize,
			                                0, 500, 15000, 4000);
			chatMessageCountSpin = new wx.SpinCtrl(this, (int) Id.chatMessageCountSpin, string.Empty,
			                                wx.Window.wxDefaultPosition, wx.Window.wxDefaultSize,
			                                0, 5, 40, 10);
			enterSendCheck = new wx.CheckBox(this, (int) Id.enterSendCheck, Catalog.GetString("'Enter' sends message"));
			enableTrayCheck = new wx.CheckBox(this, (int) Id.enableTrayCheck, Catalog.GetString("Enable tray icon"));
			confirmExitCheck = new wx.CheckBox(this, (int) Id.confirmExitCheck, Catalog.GetString("Confirm application exit"));
			showTooltipsCheck = new wx.CheckBox(this, (int) Id.showTooltipsCheck, Catalog.GetString("Show tooltips"));
			closeToTrayCheck = new wx.CheckBox(this, (int) Id.closeToTrayCheck, Catalog.GetString("Close to tray icon"));
			showNewChatCheck = new wx.CheckBox(this, (int) Id.showNewChatCheck, Catalog.GetString("Show new chat notify window"));
			saveHistoryCheck = new wx.CheckBox(this, (int) Id.saveHistoryCheck, Catalog.GetString("Save chat history"));
			loggingBox = new wx.StaticBox(this, Catalog.GetString("Logging:"));
			positionBox = new wx.StaticBox(this, Catalog.GetString("Position"));
			othersBox = new wx.StaticBox(this, Catalog.GetString("Others:"));
			mainBox = new wx.StaticBox(this, Catalog.GetString("Main window:"));
			chatBox = new wx.StaticBox(this, Catalog.GetString("Chat window:"));
		}
		
		private void DoControlsProperties()
		{
			string[] levels = { Catalog.GetString("Trace"), Catalog.GetString("Debug"), Catalog.GetString("Warn"), 
										Catalog.GetString("Info"), Catalog.GetString("Error"), Catalog.GetString("Fatal"), 
										Catalog.GetString("Off") };
			fileLoggingCombo.Append(levels);
			consoleLoggingCombo.Append(levels);
		}
		
		private void DoLayout()
		{
			topSizer = new wx.BoxSizer(wx.Orientation.wxVERTICAL);
			{
				boxSizer = new wx.BoxSizer(wx.Orientation.wxVERTICAL);
				{
					loggingSizer = new wx.StaticBoxSizer(loggingBox, wx.Orientation.wxVERTICAL);
					{
						loggingGrid = new wx.FlexGridSizer(2, 2, 2, 2);
						{
							loggingGrid.Add(fileLoggingST, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL|wx.Alignment.wxALIGN_RIGHT);
							loggingGrid.Add(fileLoggingCombo, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL);
							loggingGrid.Add(consoleLoggingST, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL|wx.Alignment.wxALIGN_RIGHT);
							loggingGrid.Add(consoleLoggingCombo, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL); 
						}
						loggingSizer.Add(loggingGrid, 1, wx.Stretch.wxEXPAND);
					}
					boxSizer.Add(loggingSizer, 0, wx.Stretch.wxEXPAND);
					
					positionSizer = new wx.StaticBoxSizer(positionBox, wx.Orientation.wxHORIZONTAL);
					{
						mainSizer = new wx.StaticBoxSizer(mainBox, wx.Orientation.wxHORIZONTAL);
						{
							mainGrid = new wx.FlexGridSizer(2, 2, 2, 2);
							{
								mainGrid.Add(mainPosXST, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL|wx.Alignment.wxALIGN_RIGHT);
								mainGrid.Add(mainPosXSpin, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL);
								mainGrid.Add(mainPosYST, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL|wx.Alignment.wxALIGN_RIGHT);
								mainGrid.Add(mainPosYSpin, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL);
							}
							mainSizer.Add(mainGrid, 1, wx.Stretch.wxEXPAND);
						}
						positionSizer.Add(mainSizer, 0, 0);
						
						
						chatSizer = new wx.StaticBoxSizer(chatBox, wx.Orientation.wxHORIZONTAL);
						{
							chatGrid = new wx.FlexGridSizer(2, 2, 2, 2);
							{
								chatGrid.Add(chatPosXST, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL|wx.Alignment.wxALIGN_RIGHT);
								chatGrid.Add(chatPosXSpin, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL);
								chatGrid.Add(chatPosYST, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL|wx.Alignment.wxALIGN_RIGHT);
								chatGrid.Add(chatPosYSpin, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL);
							}
							chatSizer.Add(chatGrid, 1, 0);
						}
						positionSizer.Add(chatSizer, 1, 0);
					}
					boxSizer.Add(positionSizer, 0, wx.Stretch.wxEXPAND|wx.Direction.wxTOP, 5);
					
					
					othersSizer = new wx.StaticBoxSizer(othersBox, wx.Orientation.wxHORIZONTAL);
					{
						othersGrid = new wx.FlexGridSizer(10, 2, 2, 2);
						{
							othersGrid.Add(enterSendCheck, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL);
							othersGrid.Add(20, 20, 0, 0, 0);
							othersGrid.Add(enableTrayCheck, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL);
							othersGrid.Add(20, 20, 0, 0, 0);
							othersGrid.Add(closeToTrayCheck, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL);
							othersGrid.Add(20, 20, 0, 0, 0);
							othersGrid.Add(confirmExitCheck, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL);
							othersGrid.Add(20, 20, 0, 0, 0);
							othersGrid.Add(showTooltipsCheck, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL);
							othersGrid.Add(20, 20, 0, 0, 0);
							othersGrid.Add(tooltipsDelayST, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL|wx.Alignment.wxALIGN_RIGHT);
							othersGrid.Add(tooltipsDelaySpin, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL);
							othersGrid.Add(showNewChatCheck, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL);
							othersGrid.Add(20, 20, 0, 0, 0);
							othersGrid.Add(notifyTtlST, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL|wx.Alignment.wxALIGN_RIGHT);
							othersGrid.Add(notifyTtlSpin, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL);
							othersGrid.Add(chatMessageCountST, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL);
							othersGrid.Add(chatMessageCountSpin, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL|wx.Alignment.wxALIGN_RIGHT);
							othersGrid.Add(saveHistoryCheck, 0, wx.Alignment.wxALIGN_CENTER_VERTICAL);
							othersGrid.Add(20, 20, 0, 0, 0);
						}
						othersSizer.Add(othersGrid, 1, 0);
					}
					boxSizer.Add(othersSizer, 1, wx.Stretch.wxEXPAND|wx.Direction.wxTOP, 5);
					
				}
				topSizer.Add(boxSizer, 1, wx.Stretch.wxEXPAND|wx.Direction.wxALL, 5);
			}
			this.Sizer = topSizer;
			topSizer.SetSizeHints(this);
		}
		
		private void OnTooltipsSpinUI(object sender, wx.Event evt)
		{
			wx.UpdateUIEvent e = (wx.UpdateUIEvent) evt;
			
			if (showTooltipsCheck.Value == true)
				e.Enabled = true;
			else
				e.Enabled = false;
		}
		
		public int FileLoggingComboSelection
		{
			get 
			{
				if (fileLoggingCombo.Selection > -1)
					return fileLoggingCombo.Selection; 
				else
				{
#if LOG
					logger.Info(Catalog.GetString("No file logging level selected, using default"));
#endif
					return (int) LogLevel.Warn;
				}
			}
			
			set 
			{
				if (value >= 0 && value < fileLoggingCombo.Count)
					fileLoggingCombo.Select(value);
				else
				{
#if LOG
					logger.Warn(String.Format(Catalog.GetString("Invalid position number: {0}"), value));
#endif
					fileLoggingCombo.Select(0);
				}
			}
		}
		
		public int ConsoleLoggingComboSelection
		{
			get 
			{
				if (consoleLoggingCombo.Selection > -1)			
					return consoleLoggingCombo.Selection; 
				else
				{
#if LOG
					logger.Info(Catalog.GetString("No console logging level selected, using default"));
#endif
					return (int) LogLevel.Warn;
				}
			}
			set 
			{
				if (value >= 0 && value < consoleLoggingCombo.Count)
					consoleLoggingCombo.Select(value);
				else
				{
#if LOG
					logger.Warn(String.Format(Catalog.GetString("Invalid position number: {0}"), value));
#endif
					consoleLoggingCombo.Select(0);
				}
			}
		}
		
		public int MainWindowXPositon
		{
			get { return mainPosXSpin.Value; }
			set 
			{
				if (value >= 0)
					mainPosXSpin.Value = value;
				else
				{
#if LOG
					logger.Warn(String.Format(Catalog.GetString("Invalid value: {0}"), value));
#endif
					mainPosXSpin.Value = 0;
				}
			}
		}
		
		public int MainWindowYPositon
		{
			get { return mainPosYSpin.Value; }
			set 
			{
				if (value >= 0)
					mainPosYSpin.Value = value;
				else
				{
#if LOG
					logger.Warn(String.Format(Catalog.GetString("Invalid value: {0}"), value));
#endif
					mainPosYSpin.Value = 0;
				}
			}
		}
		
		public int ChatWindowXPositon
		{
			get { return chatPosXSpin.Value; }
			set 
			{
				if (value >= 0)
					chatPosXSpin.Value = value;
				else
				{
#if LOG
					logger.Warn(String.Format(Catalog.GetString("Invalid value: {0}"), value));
#endif
					chatPosXSpin.Value = 0;
				}
			}
		}
		
		public int ChatWindowYPositon
		{
			get { return chatPosYSpin.Value; }
			set 
			{
				if (value >= 0)
					chatPosYSpin.Value = value;
				else
				{
#if LOG
					logger.Warn(String.Format(Catalog.GetString("Invalid value: {0}"), value));
#endif
					chatPosYSpin.Value = 0;
				}
			}
		}
		
		public bool EnterSend
		{
			get { return enterSendCheck.Value; }
			set { enterSendCheck.Value = value; }
		}
		
		public bool EnableTrayIcon
		{
			get { return enableTrayCheck.Value; }
			set { enableTrayCheck.Value = value; }
		}
		
		public bool ConfirmExit
		{
			get { return confirmExitCheck.Value; }
			set { confirmExitCheck.Value = value; }
		}
			
		public bool ShowTooltips
		{
			get { return showTooltipsCheck.Value; }
			set { showTooltipsCheck.Value = value; }
		}
		
		public int TooltipsDelay
		{
			get { return tooltipsDelaySpin.Value; }
			set 
			{
				if ( value >= 0)
					tooltipsDelaySpin.Value = value;
				else
				{
#if LOG
					logger.Warn(String.Format(Catalog.GetString("Invalid value: {0}"), value));
#endif
					tooltipsDelaySpin.Value = 2000;
				}
			}
		}
		
		public bool CloseToTray
		{
			get
			{
				if ( closeToTrayCheck.Enabled)
					return closeToTrayCheck.Value;
				else
					return false;
			}
			
			set { closeToTrayCheck.Value = value; }
		}
		
		public int ChatMessageCount
		{
			get { return chatMessageCountSpin.Value; }
			set { chatMessageCountSpin.Value = value; }
		}
		
		public bool ShowNewChatNotify
		{
			get { return showNewChatCheck.Value; }
			set { showNewChatCheck.Value = value; }
		}
		
		public int NotifyTtl
		{
			get { return notifyTtlSpin.Value; }
			set { notifyTtlSpin.Value = value; }
		}
		
		public bool SaveHistory
		{
			get { return saveHistoryCheck.Value; }
			set { saveHistoryCheck.Value = value; }
		}
	}
}
